﻿using System;
public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        tail = head = null;
    }
    public Node<T> Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }
    public void ForEach(Action<T> action)
    {
        for (Node<T> node = Head; node != null; node = node.Next)
        {
            action(node.Data);
        }
    }
}
class program
{
    static void Main(string[] args)
    {
        GenericList<int> intList = new GenericList<int>();
        for (int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }

        // 打印链表元素
        intList.ForEach(x => Console.Write(x + " "));
        Console.WriteLine();

        // 求最大值
        int max = int.MinValue;
        intList.ForEach(x => max = Math.Max(max, x));
        Console.WriteLine("Max: " + max);

        // 求最小值
        int min = int.MaxValue;
        intList.ForEach(x => min = Math.Min(min, x));
        Console.WriteLine("Min: " + min);

        // 求和
        int sum = 0;
        intList.ForEach(x => sum += x);
        Console.WriteLine("Sum: " + sum);
    }
}